package app.util.image.action
{
	import app.util.*;
	
	import flash.display.*;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.*;
	
	public class TintAction extends AbstractAction
	{
		private var fade:Number;
		private var color:uint;
		
		public function setValue(fade:Number, color:uint):TintAction
		{
			this.fade = fade;
			this.color = color;
			return this;
		}
		
		public override function getDescription():String
		{
			var colorString:String = this.color.toString(16).toUpperCase();
			while(colorString.length < 6)
			{
				colorString = "0" + colorString;
			}
			return "Tint (" + this.fade.toFixed(0) + ", #" + colorString + ")";
		}
		
		public override function getActionedBitmapData():BitmapData
		{
			var r:Number = NaN;
            var g:Number = NaN;
            var b:Number = NaN;
            r = (color >> 16 & 255) / 192;
            g = (color >> 8 & 255) / 192;
            b = (color >> 0 & 255) / 192;
			var mat:Array =  [0.3, 0.59, 0.11, 0, 42, //tint
							 0.3, 0.59, 0.11, 0, 2,
							 0.3, 0.59, 0.11, 0, 2,
							 0, 0, 0, 1, 0]; 
			mat[0] = mat[0] * r;
            mat[1] = mat[1] * r;
            mat[2] = mat[2] * r;
            mat[5] = mat[5] * g;
            mat[6] = mat[6] * g;
            mat[7] = mat[7] * g;
            mat[10] = mat[10] * b;
            mat[11] = mat[11] * b;
            mat[12] = mat[12] * b;
            mat[18] = 1;
			var old:BitmapData = Mediator.getCurrentBitmapData();
			var ret:BitmapData = old.clone();
			var filter:ColorMatrixFilter = new ColorMatrixFilter(mat);
			ret.applyFilter(ret, ret.rect, new Point(0, 0), filter);
			ret.merge(old, ret.rect, new Point(0,0), 256-fade,
				256-fade, 256-fade, 256-fade);
			return ret;
		}	
	
		
		
	}
}